package com.googlecode.continuity.plugin.project.server.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ParameterizableViewController;
import org.springframework.web.servlet.view.RedirectView;
import com.googlecode.continuity.core.server.mvc.ContinuityModel;
import com.googlecode.continuity.plugin.project.client.Milestone;
import com.googlecode.continuity.plugin.project.server.service.ProjectService;

/**
 * ProjectViewController
 *
 * @author Peter Schmitt <aragos@aragos.de>
 */
public class MilestoneViewController extends ParameterizableViewController {
// ------------------------------ FIELDS ------------------------------

  /**
   * Logger for this class and subclasses
   */
  protected final Log logger = LogFactory.getLog(getClass());
  private ProjectService projectService;
  private String listURL;


// --------------------- GETTER / SETTER METHODS ---------------------
  public String getListURL() {
    return listURL;
  }

  public void setListURL(String listURL) {
    this.listURL = listURL;
  }// --------------------- GETTER / SETTER METHODS ---------------------

  public void setProjectService(ProjectService projectService) {
    this.projectService = projectService;
  }

// -------------------------- OTHER METHODS --------------------------

  public ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    ContinuityModel model = new ContinuityModel();

    int milestoneID = Integer.parseInt(request.getParameter("id"));
    Milestone milestone = projectService.getMilestoneByID(milestoneID);

    // if project doesn't exist show error and list mode
    // todo: include error
    if (milestone == null) {
      return new ModelAndView(new RedirectView(getListURL(), true));
    }

    model.put("milestone", milestone);

    return new ModelAndView(getViewName(), model);
  }
}